package com.dianxin.farm.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianxin.farm.entity.CustomerBalance;
import com.dianxin.farm.entity.CustomerInfo;
import com.dianxin.farm.entity.WithdrawBill;
import com.dianxin.farm.entity.pojo.WithdrawPageResponse;
import com.dianxin.farm.filter.AuthorCheck;
import com.dianxin.farm.mapper.bill.WithdrawBillMapper;
import com.dianxin.farm.mapper.customer.CustomerBalanceMapper;
import com.dianxin.farm.request.common.PageRequest;
import com.dianxin.farm.request.withdraw.WithdrawRequest;
import com.dianxin.farm.service.CurrentLoginService;
import com.dianxin.farm.util.exception.UserOperationException;
import com.dianxin.farm.util.order.Generator;
import com.dianxin.farm.util.order.IdGeneratorFactory;
import com.dianxin.farm.util.web.ApiResponse;
import com.dianxin.farm.util.web.ApiResponseUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 橱窗信息表 前端控制器
 * </p>
 *
 * @author xukai
 * @since 2020-09-14
 */
@RestController
@RequestMapping(value = "/withdrawBill", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
@AuthorCheck
@Api(description = "提现表单控制器")
public class WithdrawController {


    private static final Generator generate= IdGeneratorFactory.create("withdraw_bill");
    @Resource
    private WithdrawBillMapper withdrawBillMapper;
    @Resource
    private CurrentLoginService currentLoginService;
    @Resource
    private CustomerBalanceMapper customerBalanceMapper;

    @ApiOperation("申请提现")
    @RequestMapping(value = "/createWithdraw")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<Map<String,String>> bannerPages(@Valid @RequestBody WithdrawRequest request) {
        BigDecimal money=request.getMoney();
        String remark=request.getRemark();
        if (money.compareTo(new BigDecimal("0"))!=1){
            throw new UserOperationException("请填写正确提现金额");
        }
        CustomerInfo customerInfo=currentLoginService.getCurrentInfo();
        Integer customerId=customerInfo.getCustomerId();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("customer_id",customerId);
        CustomerBalance customerBalance=customerBalanceMapper.selectOne(wrapper);

        if (customerBalance!=null){
            BigDecimal balance=customerBalance.getBalance();
            if (balance.compareTo(money)==-1){
                throw new UserOperationException("账户余额不足");
            }
        }

        WithdrawBill bill=new WithdrawBill();
        bill.setCreateTime(LocalDateTime.now());
        bill.setCustomerId( customerId);
        bill.setMoney(money);
        bill.setStatus(0);
        bill.setBillCode(generate.generate());
        withdrawBillMapper.insert(bill);

        Map<String,String> map=new HashMap<>();
        map.put("billId",bill.getId().toString());

        return ApiResponseUtils.success(map);
    }

    @ApiOperation("我的提现记录")
    @RequestMapping(value = "/myWithdrawPage")
    @ResponseBody
    @AuthorCheck
    @Transactional
    public ApiResponse<PageInfo<WithdrawPageResponse>> myWithdrawPage(@Valid @RequestBody PageRequest request) {
        Integer customerId=currentLoginService.getId();
        PageHelper.startPage(request.getPage(),request.getSize());
        List<WithdrawPageResponse> withdrawBillList=withdrawBillMapper.selectByCondition(customerId,null,null,null,null);
        return ApiResponseUtils.success(new PageInfo<>(withdrawBillList));

    }


}
